#include "node_parser.h"
#include <iostream>
#include <sstream>

bool NodeParser::ParseNode(const std::string& json, nanok::Node& node) {
    Json::Value root;
    Json::CharReaderBuilder builder;
    std::istringstream iss(json);
    std::string errs;
    if (!Json::parseFromStream(builder, iss, &root, &errs)) {
        std::cerr << "Failed to parse JSON: " << errs << std::endl;
        return false;
    }
    try {
        // 解析 metadata
        if (root.isMember("metadata")) {
            Json::Value metadata = root["metadata"];
            node.metadata.name = metadata["name"].asString();
            node.metadata.namespace_ = metadata["namespace"].asString();
            node.metadata.resourceVersion = metadata["resourceVersion"].asInt64();
        }
        // 解析 spec
        node.spec.podCIDR = root["spec"]["podCIDR"].asString();
        // 解析status
        for (const auto& key : root["status"]["capacity"].getMemberNames()) {
            node.status.capacity[key] = root["status"]["capacity"][key].asString();
        }
        for (const auto& key : root["status"]["allocatable"].getMemberNames()) {
            node.status.allocatable[key] = root["status"]["allocatable"][key].asString();
        }
        return true;
    } catch (const std::exception& e) {
        std::cerr << "Error parsing Pod: " << e.what() << std::endl;
        return false;
    }
}

bool NodeParser::ParseNodeList(const std::string& json, std::vector<nanok::Node>& nodes) {
    Json::Value root;
    Json::CharReaderBuilder builder;
    std::istringstream iss(json);
    std::string errs;
    if (!Json::parseFromStream(builder, iss, &root, &errs)) {
        std::cerr << "Failed to parse JSON: " << errs << std::endl;
        return false;
    }
    try {
        // 检查是否是数组
        if (root.isArray()) {
            for (const auto& item : root) {
                nanok::Node node;
                // 将每个数组元素转换为字符串并解析为 Pod
                Json::StreamWriterBuilder writer;
                std::string nodeJson = Json::writeString(writer, item);
                if (ParseNode(nodeJson, node)) {
                    nodes.push_back(node);
                }
            }
            return true;
        }
        // 如果不是数组，尝试解析为单个 Pod
        nanok::Node node;
        if (ParseNode(json, node)) {
            nodes.push_back(node);
            return true;
        }
        return false;
    } catch (const std::exception& e) {
        std::cerr << "Error parsing Pod list: " << e.what() << std::endl;
        return false;
    }
}

std::string NodeParser::ToJson(const nanok::Node& node) {
    Json::Value root;
    // 构建 metadata
    Json::Value metadata;
    metadata["name"] = node.metadata.name;
    metadata["namespace"] = node.metadata.namespace_;
    metadata["resourceVersion"] = node.metadata.resourceVersion;
    root["metadata"] = metadata;
    // 构建 spec
    Json::Value spec;
    spec["podCIDR"] = node.spec.podCIDR;
    root["spec"] = spec;
    // 构建 status
    Json::Value status;
    Json::Value capacityJson(Json::objectValue);
    for (const auto& pair : node.status.capacity) {
        capacityJson[pair.first] = pair.second;
    }
    status["capacity"] = capacityJson;
    Json::Value allocatableJson(Json::objectValue);
    for (const auto& pair : node.status.allocatable) {
        allocatableJson[pair.first] = pair.second;
    }
    status["allocatable"] = allocatableJson;
    root["status"] = status;
    Json::StreamWriterBuilder builder;
    return Json::writeString(builder, root);
}